﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.MailClient.Win.Forms.Data;
using DevExpress.XtraGrid.Views.Base;
using Zero.DataLayer;
using Zero.DataLayer.Repository;
using Zero.POCO.Enum;
using Zero.POCO.Enum.Helper.Support;
using Zero.POCO.Helper.Support;
using Zero.POCO.Support;
using Zero.Presenter;
using Zero.Presenter.Donnees;

namespace DevExpress.MailClient.Win.Modules.Donnees
{
    public partial class Tarifs :  BaseModule,ITarifView
    {
        private int focusedRow = 0;
        public List<TarifView> tarifs { get; set; }
        public List<SupportTV> SupportTvs { get; set; }
        public List<SupportPresse> SupportPresses { get; set; }
        public List<SupportRadio> SupportRadios { get; set; }

        public TypeMedia media { get; set; }
        private TarifsPresenter tarifsPresenter;
        public Tarifs()
        {
            InitializeComponent();
            tarifsPresenter = new TarifsPresenter(this, new TarifTVRepository(),new TarifRdRepository(), new TarifPrRepository(),new SupportTvRepository(), new SupportRadioRepository(), new SupportPresseRepository());
            tarifsPresenter.Display();
            gridTarif.DataSource = tarifs;
            var firstOrDefault = tarifs.FirstOrDefault();
            if (firstOrDefault != null && (tarifs!=null && tarifs.Any() && firstOrDefault.lignetarif!=null && firstOrDefault.lignetarif.Any()))
            {
                var orDefault = tarifs.FirstOrDefault();
                if (orDefault != null) gcLigneTarifs.DataSource = orDefault.lignetarif;
            }
            gvTarif.Columns[0].Visible = false;
            var tarifView = tarifs.FirstOrDefault();
            if (tarifView != null)
                gcLigneTarifs.DataSource = tarifs.Any() ? tarifView.lignetarif : new List<LignesTarif>();
        }
        private void bbiAdd_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditLigneTarifs(null,tarifs,true, focusedRow);
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.TopMost = true;
            form.ShowDialog();
            
            form.Activate();
            if (form.DialogResult != DialogResult.Yes && form.DialogResult != DialogResult.OK) return;
            var resultat = form.LigneTarif;
            var tarifResultat = form.tarifview;
            if(form.DialogResult== DialogResult.Abort) return;
            tarifResultat.lignetarif=new List<LignesTarif>();tarifResultat.lignetarif.Add(resultat);
            tarifsPresenter.WriteChanges(tarifResultat);

            tarifsPresenter.Display();
            gridTarif.DataSource = tarifs;
            var firstOrDefault = tarifs.FirstOrDefault();
            if (firstOrDefault != null && (tarifs != null && tarifs.Any() && firstOrDefault.lignetarif != null))
            {
                var orDefault = tarifs.FirstOrDefault();
                if (orDefault != null) gcLigneTarifs.DataSource = orDefault.lignetarif;
            }
            gvTarif.Columns[0].Visible = false;


            
            if (tarifs.Any())
            {
                var orDefault = tarifs.FirstOrDefault();
                if (orDefault != null) gcLigneTarifs.DataSource = orDefault.lignetarif;
                gvLigneTarif.Columns[0].Visible = false;
                var tarifView = tarifs.FirstOrDefault();
                if (tarifView != null && tarifView.media != TypeMedia.PR)
                {

                    gvLigneTarif.Columns[2].Visible = true;
                    gvLigneTarif.Columns[3].Visible = true;
                }
                else
                {

                    gvLigneTarif.Columns[2].Visible = false;
                    gvLigneTarif.Columns[3].Visible = false;
                }
            }
            else
            {
                gcLigneTarifs.DataSource = new List<LignesTarif>();
                gvLigneTarif.Columns[0].Visible = false;
                gvLigneTarif.Columns[2].Visible = false;
                gvLigneTarif.Columns[3].Visible = false;
            }



            //var tarifslect = tarifs.First(ed => ed.id == tarifResultat.id).lignetarif;
            //if(tarifslect==null) tarifslect=new List<LignesTarif>();
            //tarifslect.Add(resultat);
            //gcLigneTarifs.Focus();
            Cursor.Current = Cursors.Default;
        }
        private void bbiModify_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            }

        private void bbiDelete_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            
        }

        private void bbiAddTarifs_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            var form = new frmEditTarif(null, true, SupportTvs, SupportPresses, SupportRadios);
            form.Location = new Point(Left + (Width - form.Width) / 2, Top + (Height - form.Height) / 2);
            form.TopMost = true;
            form.ShowDialog();
            form.TopMost = true;
            form.Activate();
            var resultat = form.TarfView;
            if (resultat != null)
            {
                tarifs.Add(resultat);
                tarifsPresenter.WriteChanges(resultat);
                tarifsPresenter.Display();
                gridTarif.DataSource = tarifs;
                var firstOrDefault = tarifs.FirstOrDefault();
                if (firstOrDefault != null && (tarifs != null && tarifs.Any() && firstOrDefault.lignetarif != null))
                {
                    var orDefault = tarifs.FirstOrDefault();
                    if (orDefault != null) gcLigneTarifs.DataSource = orDefault.lignetarif;
                }
                gvTarif.Columns[0].Visible = false;



                if (tarifs.Any())
                {
                    var orDefault = tarifs.FirstOrDefault();
                    if (orDefault != null) gcLigneTarifs.DataSource = orDefault.lignetarif;
                    gvLigneTarif.Columns[0].Visible = false;
                    var tarifView = tarifs.FirstOrDefault();
                    if (tarifView != null && tarifView.media != TypeMedia.PR)
                    {

                        gvLigneTarif.Columns[2].Visible = true;
                        gvLigneTarif.Columns[3].Visible = true;
                    }
                    else
                    {

                        gvLigneTarif.Columns[2].Visible = false;
                        gvLigneTarif.Columns[3].Visible = false;
                    }
                }
                else
                {
                    gcLigneTarifs.DataSource = new List<LignesTarif>();
                    gvLigneTarif.Columns[0].Visible = false;
                    gvLigneTarif.Columns[2].Visible = false;
                    gvLigneTarif.Columns[3].Visible = false;
                }
            }
            Cursor.Current = Cursors.Default;
        }private void BBIModifyTarifs_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }

        private void bbiDeleteTarif_ItemClick(object sender, XtraBars.ItemClickEventArgs e)
        {

        }
        private void gvLigneTarif_InitNewRow(object sender, XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            ColumnView View = sender as ColumnView;
            if (View != null)
            {
                View.SetRowCellValue(e.RowHandle, View.Columns["Type"], true);
                View.SetRowCellValue(e.RowHandle, View.Columns["CreatedDate"], DateTime.Today);
            }
        }

        private void gvTarif_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                focusedRow = e.FocusedRowHandle;
                  media=((TarifView)gvTarif.GetRow(e.FocusedRowHandle)).media;

                var tarif=((TarifView)gvTarif.GetRow(e.FocusedRowHandle));
                if (tarif.lignetarif != null)
                {
                    gcLigneTarifs.DataSource = tarif.lignetarif;
                    gvLigneTarif.Columns[0].Visible = false;
                    if (tarif.media != TypeMedia.PR)
                    {

                        gvLigneTarif.Columns[2].Visible = true;
                        gvLigneTarif.Columns[3].Visible = true;
                    }
                    else
                    {

                        gvLigneTarif.Columns[2].Visible = false;
                        gvLigneTarif.Columns[3].Visible = false;
                    }
                }
                else
                {
                    gcLigneTarifs.DataSource = new List<LignesTarif>();
                    gvLigneTarif.Columns[0].Visible = false;
                    if (tarif.media != TypeMedia.PR)
                    {

                        gvLigneTarif.Columns[2].Visible = true;
                        gvLigneTarif.Columns[3].Visible = true;
                    }
                    else
                    {

                        gvLigneTarif.Columns[2].Visible = false;
                        gvLigneTarif.Columns[3].Visible = false;
                    }
                }

            }
        }

        private void gvLigneTarif_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.FieldName == "Format")
                switch (media)
                {
                    case TypeMedia.TV:
                    {
                        if ((int) e.Value == 0) e.DisplayText = "Tout format";
                        else e.DisplayText = (int) e.Value*5 + " Secondes";
                        break;
                    }
                    case TypeMedia.RD:
                    {
                        if ((int) e.Value == 0) e.DisplayText = "Tout format";
                        else e.DisplayText = (int) e.Value*5 + " Secondes";
                        break;
                    }
                    case TypeMedia.PR:
                    {
                        e.DisplayText = "" + (FormatPresse) e.Value;
                        break;
                    }

                }
            if (e.Column.FieldName == "JourSemaine")
            {
                e.DisplayText = ""+(JourSemaine)e.Value;
            }

        }
    }}